﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.ComponentModel;

using FXnaAppBase;


namespace FXnaAppBaseWpf
{
    /// <summary>
    /// winRun.xaml 的交互逻辑
    /// </summary>
    public partial class winRun : Window
    {
        public winRun(RunData data)
        {
            InitializeComponent();

            this._runData = data;

        }



        private void BackgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            //注意这里是可以输入参数的，我们要把计算对象作为参数传递进去
            RunData data = e.Argument as RunData;

            DosService ds = new DosService(this._backgroudnWorker);
            ds.Run(data);
            


        }

        private void BackgroundWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            if (e.Error!=null)
            {
                MessageBox.Show(e.Error.Message,"错误");
                
            }

        }

        private void BackgroundWorker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            if (e.ProgressPercentage==0)
            {

                string str = e.UserState as string;

                this.tbMessage.Text = str;

                
            }

            else if (e.ProgressPercentage==1)
            {

                string str = e.UserState as string;

                this.tbMessage.Text = str;

                
            }


          


        }

        private void btnRun_Click(object sender, RoutedEventArgs e)
        {
            if (!this._backgroudnWorker.IsBusy)
            {

                //this.btnRun.IsEnabled = false;
                //this.btnCancel.IsEnabled = true;


                this._backgroudnWorker.RunWorkerAsync(this._runData);



            }
            else
            {

                MessageBox.Show("当前实例正在运行中！需要先终止计算");
            }

            

        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            if (this._backgroudnWorker.IsBusy)
            {

                this._backgroudnWorker.CancelAsync();
                
            }

          

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this._backgroudnWorker= this.FindResource("bwRun") as BackgroundWorker;
        }



        BackgroundWorker _backgroudnWorker;
        RunData _runData;


    }
}
